Skip to contentMethod: SoqlQueryParser(QueryParser, MetamodelImpl)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.soql;
16:
17: import cz.cvut.kbss.jopa.model.MetamodelImpl;
18: import cz.cvut.kbss.jopa.query.QueryHolder;
19: import cz.cvut.kbss.jopa.query.QueryParser;
20: import org.antlr.v4.runtime.CharStream;
21: import org.antlr.v4.runtime.CharStreams;
22: import org.antlr.v4.runtime.CommonTokenStream;
23: import org.antlr.v4.runtime.tree.ParseTree;
24: import org.antlr.v4.runtime.tree.ParseTreeWalker;
25:
26: public class SoqlQueryParser implements QueryParser {
27:
28: private final QueryParser sparqlParser;
29: private final MetamodelImpl metamodel;
30:
31: public SoqlQueryParser(QueryParser sparqlParser, MetamodelImpl metamodel) {
32: this.sparqlParser = sparqlParser;
33: this.metamodel = metamodel;
34: }
35:
36:
37: @Override
38: public QueryHolder parseQuery(String query) {
39: CharStream cs = CharStreams.fromString(query);
40: SoqlLexer lexer = new SoqlLexer(cs);
41: final CommonTokenStream tokens = new CommonTokenStream(lexer);
42: SoqlParser parser = new SoqlParser(tokens);
43:
44: final ParseTree tree = parser.querySentence();
45: final SoqlQueryListener listener = new SoqlQueryListener(this.metamodel);
46:
47: final ParseTreeWalker walker = new ParseTreeWalker();
48: walker.walk(listener, tree);
49:
50: return sparqlParser.parseQuery(listener.getSoqlQuery());
51: }
52: }